﻿using Microsoft.Extensions.DependencyInjection;
using QPlcPlayer.Models;
using QPlcPlayer.Services;
using S7.Net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QPlcPlayer
{
	public class QPlcManager
	{

		Plc m_plc;

		public QPlcManager()
		{
			var info = AppRuntime.GetService().GetRequiredService<AgvCarService>().GetAgvCarByNumber(AppRuntime.CarNumber);
			m_plc = new Plc((CpuType)info.PlcType, info.IP, 0, 1);
		}
		public QPlcManager(AgvCar info)
		{
			m_plc = new Plc((CpuType)info.PlcType, info.IP, 0, 1);
		}
		public Plc GetPlc()
		{
			if(m_plc == null)
			{
				var info=AppRuntime.GetService().GetRequiredService<AgvCarService>().GetAgvCarByNumber(AppRuntime.CarNumber);
				m_plc = new Plc((CpuType)info.PlcType, info.IP, 0, 1);
			}
			if(!m_plc.IsConnected)m_plc.Open();
			return m_plc;
		}


		public bool 开机()
		{
			var res = (bool)GetPlc().Read(QplcAddress.开机);

			return res;
		}


		public bool 充电()
		{
			var res = (bool)GetPlc().Read(QplcAddress.充电);
			return res;
		}
		public bool 通讯正常()
		{
			var res = (bool)GetPlc().Read(QplcAddress.通讯正常);
			return res;
		}
		public bool 故障全部()
		{
			var res = (bool)GetPlc().Read(QplcAddress.故障全部);
			return res;
		}
		public bool 喂料中()
		{
			var res = (bool)GetPlc().Read(QplcAddress.喂料中);
			return res;
		}
		public bool 本次喂料完成()
		{
			var res = (bool)GetPlc().Read(QplcAddress.本次喂料完成);
			return res;
		}
		public bool 故障左行走()
		{
			var res = (bool)GetPlc().Read(QplcAddress.故障左行走);
			return res;
		}
		public bool 故障右行走()
		{
			var res = (bool)GetPlc().Read(QplcAddress.故障右行走);
			return res;
		}
		public bool 故障左喂料()
		{
			var res = (bool)GetPlc().Read(QplcAddress.故障左喂料);
			return res;
		}
		public bool 故障右喂料()
		{
			var res = (bool)GetPlc().Read(QplcAddress.故障右喂料);
			return res;
		}
		public bool 小车急停()
		{
			var res = (bool)GetPlc().Read(QplcAddress.小车急停);
			return res;
		}
		public bool 电池报警()
		{
			var res = (bool)GetPlc().Read(QplcAddress.电池报警);
			return res;
		}
		public bool 左出轨()
		{
			var res = (bool)GetPlc().Read(QplcAddress.左出轨);
			return res;
		}
		public bool 右出轨()
		{
			var res = (bool)GetPlc().Read(QplcAddress.右出轨);
			return res;
		}
		public bool 弯道超时()
		{
			var res = (bool)GetPlc().Read(QplcAddress.弯道超时);
			return res;
		}
		public bool 前有障碍物()
		{
			var res = (bool)GetPlc().Read(QplcAddress.前有障碍物);
			return res;
		}


		public (int YEAR, int MONTH, int DAY, int WEEKDAY, int HOUR, int MINUTE, int SECOND, int NANOSECOND) GetDateTime()
		{
			var plc = GetPlc();
			var year = (short)plc.Read(DataType.DataBlock, 1000, 2, VarType.Int, 1);
			var month= (byte)plc.Read(DataType.DataBlock, 1000,4, VarType.Byte, 1);
			var day = (byte)plc.Read(DataType.DataBlock, 1000, 5, VarType.Byte, 1);
			var weekday = (byte)plc.Read(DataType.DataBlock, 1000, 6, VarType.Byte, 1);
			var hour = (byte)plc.Read(DataType.DataBlock, 1000, 7, VarType.Byte, 1);
			var minute = (byte)plc.Read(DataType.DataBlock, 1000, 8, VarType.Byte, 1);
			var second = (byte)plc.Read(DataType.DataBlock, 1000, 9, VarType.Byte, 1);
			var nanosecond = (int)plc.Read(DataType.DataBlock, 1000, 9, VarType.DInt, 1);


			return (year, month, day, weekday, hour, minute, second, nanosecond);
		}


		public string GetBarCode()
		{
			var plc = GetPlc();
			var xx = plc.Read(DataType.DataBlock, 1000, 14, VarType.String, 36);		
			var res=xx.ToString();
			return res;
		
		}
		public string 当前喂料笼数()
		{
			var plc = GetPlc();
			var res = plc.Read(DataType.DataBlock, 1000, 50, VarType.DWord, 1).ToString();

			return res;
		}
		
		public string 当前电量()
		{
			var plc = GetPlc();
			var res = plc.Read(DataType.DataBlock, 1000, 54, VarType.DWord, 1).ToString();

			return res;
		}


		#region  写入数据

		public void 启动小车()
		{
			var plc = GetPlc();
			plc.Write(DataType.DataBlock, 1000, 0, 1,0);
		}
		
		public void 上位机下达允许喂料(bool IsOK)
		{
			var plc = GetPlc();
			var value=IsOK? 1 : 0;
			plc.Write(DataType.DataBlock, 1000, 76, value,0);
		}
		public void 上位机下达急停(bool IsOk)
		{
			var plc = GetPlc();
			var value = IsOk ? 1 : 0;
			plc.Write(DataType.DataBlock, 1000, 76, value,1);
		}
		public void 上位机下达早喂料开始时间(DosingSchedule info)
		{
			var plc = GetPlc();
			var seonds = info.HourFirst * 60 * 60 + info.MinuteFirst * 60;
			plc.Write(DataType.DataBlock, 1000, 64, seonds);
		}
		public void 上位机下达中喂料开始时间(DosingSchedule info)
		{
			var plc = GetPlc();
			var seonds = info.HourSecond * 60 * 60 + info.MinuteSecond * 60;
			plc.Write(DataType.DataBlock, 1000, 68, seonds);
		}
		public void 上位机下达晚喂料开始时间(DosingSchedule info)
		{
			var plc = GetPlc();
			var seonds = info.HourThird * 60 * 60 + info.MinuteThird * 60;
			plc.Write(DataType.DataBlock, 1000, 72, seonds);
		}

		public void 上位机下达喂料开始时间(DosingSchedule info)
		{
			var plc = GetPlc();
			var seonds_0 = info.HourFirst * 60 * 60 + info.MinuteFirst * 60;
			var seonds_1 = info.HourSecond * 60 * 60 + info.MinuteSecond * 60;
			var seonds_2 = info.HourThird * 60 * 60 + info.MinuteThird * 60;
			plc.Write(DataType.DataBlock, 1000, 64, seonds_0);
			plc.Write(DataType.DataBlock, 1000, 68, seonds_0);
			plc.Write(DataType.DataBlock, 1000, 72, seonds_0);
		}
		public void 上位机喂料量下达(int[] values)
		{
			var plc = GetPlc();
			plc.Write(DataType.DataBlock, 1000, 78, values);
			
		}


		#endregion

	}
}
